/**
 *  我的收文 具体公文
 */
var doc_id;

$(document).ready(function () {

    //获得参数的方法
    var request =
        {
            QueryString : function(val)
            {
                var uri = window.location.search;
                var re = new RegExp("" +val+ "=([^&?]*)", "ig");
                return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
            }
        }
    doc_id = request.QueryString("doc_id");

    //iframe自适应
    function iFrameHeight() {
        var ifm= document.getElementById("documentIframe");
        var subWeb = document.frames ? document.frames["documentIframe"].document : ifm.contentDocument;
        if(ifm != null && subWeb != null) {
            ifm.width = subWeb.body.scrollWidth;
        }
    }

    // 查看是否具有相关权限查看公文
    $.ajax({
        type:'POST',
        url:'../document/receive/detailed/'+ doc_id,
        contentType: 'application/json;',
        dataType:'JSON',
        success:function (json,status) {
            if(json.err_msg!=null){
                alert(json.err_msg);
            }else{
                //调用方法获得参数
                if(json.data!=null){
                    $("#documentIframe").css("display","block");
                    iFrameHeight();
                    $("#documentIframe").attr("src","../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/receive/read/" + doc_id);
                }else{
                    $("#draft_pdf").css("display","none");
                    $("#documentDown").css("display","none");
                }
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

        },
        fail:function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
            alert("fail!");
        },
        cache:false
    });


    // 获取所有附件列表
    $.ajax({
        dataType: 'JSON',
        type: 'POST',
        url: '../document/receive/annex/list/' + doc_id,
        success: function (json, status) {
            if (json.err_msg != null) {
                alert(json.err_msg);
            } else {
                var documentAnnex = json.data.documentAnnex;
                for (var i = 0; i < documentAnnex.length; i++) {
                    $("#datatable").append("<tr>" +
                        "<td>" + documentAnnex[i].name + "</td>" +
                        "<td>" + documentAnnex[i].writer + "</td>" +
                        "<td><button type='button' class='btn btn-success' onclick='downFujian(" + documentAnnex[i].id + ")'>下载</button>" +
                        "</td>");
                }
                //console.log(json.data.documentAnnex);d
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    })
});

function downFujian(id) {
    window.location.href="../document/receive/annex/down/" + doc_id + "/" + id;
}

$("#documentDown").click(function () {
    window.location.href='../document/receive/down/' + doc_id;
});